/*
* @(#)FileDownloader.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.core;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.TextUtility;

/**
 * <p>Class Name:       FileDownloader.java    </p>
 * <p>Description:      FileDownloader handles the document downloading from DMS. </p>
 *
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       01/08/2003
 * @version             $Revision: 1.12.4.1 $
 */

public class FileDownloader {

  public static final String REVISION = "$Revision: 1.12.4.1 $";

  public static int DATA_BLOCK_SIZE = 8192;

  protected Log log = LogFactory.getLog(this.getClass().getName());
  protected HttpServletResponse response;
  protected HttpServletRequest request;
  protected String filename = "";
  protected InputStream attachment;       //InputStream for temporarily storing attachment
  protected String contentType;
  protected byte[] b;
  protected InputStream dataStream;
  protected boolean isDownload = false;

  public FileDownloader() {
  }

  public void setIsDownload(boolean flag) {
    this.isDownload = flag;
  }

  public void setResponse(HttpServletResponse response) {
    this.response = response;
  }

  public void setRequest(HttpServletRequest request) {
    this.request = request;
  }

  public void setFileName(String filename) {
    try {
      this.filename = filename;
//      this.filename = URLDecoder.decode(filename, "UTF-8");
    } catch (Exception e) {
      log.error(e, e);
    }
  }

  public void setContentType(String contentType) {
    this.contentType = contentType;
  }

  public void setBytesContent(byte[] b) {
    this.b = b;
  }

  public void setInputStream(InputStream dataStream) {
    this.dataStream = dataStream;
  }

  public InputStream getInputStream() {
    if (b != null) {
      return new ByteArrayInputStream(b);
    }
    else {
      return dataStream;
    }
  }

  /**
   * Sets the HttpServletResponse for downloading and the javax.servlet.ServletOutputStream 
   * for downloading.
   * 
   * @throws ApplicationException
   * @see com.dcivision.framework.ErrorConstant
   */
  public void operate() throws ApplicationException {
    InputStream inputStream = null;
    try {
      filename = TextUtility.replaceString(filename, "+", "%20");
      //response.addHeader("Content-Transfer-Encoding", "base64");
      /*
       * Update Code Start
       * Author			Jim.Zhou
       * Date 			2006/04/10
       * Bug				#4793
       */
      if (this.isDownload) {
        response.addHeader("Content-Disposition", "attachment; filename=\"" + filename  + "\"");    //addheader so that filename can auto put into the download dialog box
      } else {
        response.addHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");    //addheader so that filename can auto put into the download dialog box
      }
      /*
       * Update Code End
       */
      response.setContentType(contentType);                                       //set the file type default unrecognized so that other browser can download
      ServletOutputStream sos = response.getOutputStream();                       //put the outputstream to client

      int bufferSize = 8192;
      byte[] buffer = new byte[bufferSize];
      int length = -1;
      inputStream = this.getInputStream();

      while ((length = inputStream.read(buffer, 0, bufferSize)) != -1) {
        sos.write(buffer, 0, length);
      }

      sos.close();
    }
    catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(com.dcivision.framework.ErrorConstant.COMMON_FATAL_ERROR, e);
    }
    finally {
      try { inputStream.close(); } catch (IOException ignore) {}
    }
  }
}
